/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package per.wsj.library;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Rating;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.VectorElement;
import ohos.app.Context;
import per.wsj.library.util.ElementColorUtil;
import per.wsj.library.util.TypedAttrUtils;

/**
 * AndRatingBar实现类
 *
 * @since 2021-04-13
 */
public class AndRatingBar extends Rating {
    /**
     * star step
     */
    private float starStep;

    /**
     * right to left
     */
    private boolean isRight2Left;

    /**
     * fill star bg
     */
    private Element mDrawable;

    /**
     * null star bg
     */
    private Element mBgDrawable;

    /**
     * half star bg
     */
    private Element mHalfDrawable;

    /**
     * right2Left half star bg
     */
    private Element mRight2LeftHalfDrawable;

    /**
     * default fill star bg
     */
    private VectorElement mDefStar;

    /**
     * default null star bg
     */
    private VectorElement mDefBgStar;

    /**
     * default star bg half
     */
    private VectorElement mDefBgStarHalf;

    /**
     * default star bg half RightToLeft
     */
    private VectorElement mDefBgStarHalfRightToLeft;

    /**
     * default fill star bg color
     */
    private int mStarColor;

    /**
     * default null star bg color
     */
    private int mBgColor;

    /**
     * width/height per
     */
    private float per;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public AndRatingBar(Context context) {
        this(context, null);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性值
     */
    public AndRatingBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet, "");
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性值
     * @param styleName 样式名
     */
    public AndRatingBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet, styleName);
    }

    private void init(Context context, AttrSet attrs, String styleName) {
        mDefStar = new VectorElement(context, ResourceTable.Graphic_ic_rating_star_solid);
        mDefStar.setAntiAlias(true);
        mDefBgStar = new VectorElement(context, ResourceTable.Graphic_ic_rating_star_border);
        mDefBgStar.setAntiAlias(true);
        mDefBgStarHalf = new VectorElement(context, ResourceTable.Graphic_ic_rating_star_half);
        mDefBgStarHalf.setAntiAlias(true);
        mDefBgStarHalfRightToLeft = new VectorElement(context, ResourceTable.Graphic_ic_rating_star_half_rightToLeft);
        mDefBgStarHalfRightToLeft.setAntiAlias(true);

        isRight2Left = TypedAttrUtils.getBoolean(attrs, "right2Left", false);
        mDrawable = TypedAttrUtils.getElement(attrs, "starDrawable", mDefStar);
        mBgDrawable = TypedAttrUtils.getElement(attrs, "bgDrawable", mDefBgStar);
        mHalfDrawable = TypedAttrUtils.getElement(attrs, "halfStarDrawable", mDefBgStarHalf);
        mRight2LeftHalfDrawable = TypedAttrUtils.getElement(attrs, "right2LeftHalfDrawable", mDefBgStarHalfRightToLeft);
        starStep = TypedAttrUtils.getFloat(attrs, "starStep", 1);

        mStarColor = TypedAttrUtils.getIntColor(attrs, "starColor", 0);
        mBgColor = TypedAttrUtils.getIntColor(attrs, "bgColor", 0);
        if (mStarColor != 0 && mBgColor != 0) {
            mDrawable = ElementColorUtil.getTintElement(mDrawable, mStarColor);
            mBgDrawable = ElementColorUtil.getTintElement(mBgDrawable, mBgColor);
        }

        setGrainSize(starStep);

        per = mDrawable.getWidth() / mDrawable.getHeight();
        int height = getHeight();
        int width = (int) (height * per);
        mDrawable.setBounds(0, 0, width, height);
        mBgDrawable.setBounds(0, 0, width, height);
        mHalfDrawable.setBounds(0, 0, width, height);
        mRight2LeftHalfDrawable.setBounds(0, 0, width, height);

        if (isRight2Left) {
            setRating(getRatingItems() - getScore());
            setFilledElement(mBgDrawable);
            setUnfilledElement(mDrawable);
            setHalfFilledElement(mRight2LeftHalfDrawable);
        } else {
            setFilledElement(mDrawable);
            setUnfilledElement(mBgDrawable);
            setHalfFilledElement(mHalfDrawable);
        }
    }

    @Override
    public float getScore() {
        if (isRight2Left) {
            return getRatingItems() - super.getScore();
        } else {
            return super.getScore();
        }
    }

    @Override
    public void setScore(float score) {
        if (isRight2Left) {
            super.setScore(getRatingItems() - score);
        } else {
            super.setScore(score);
        }
    }

    /**
     * Sets the rating (the number of stars filled).
     *
     * @param rating The rating to set.
     */
    public void setRating(float rating) {
        setScore(rating);
    }

    /**
     * set the scale factor of the ratingbar
     *
     * @param scaleFactor scaleFactor
     */
    public void setScaleFactor(float scaleFactor) {
        postLayout();
    }

    /**
     * set the spacing of the star
     *
     * @param starSpacing starSpacing
     */
    public void setStarSpacing(float starSpacing) {
        postLayout();
    }
}